package tv.huan.web.controller.media;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.core.text.Convert;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.framework.util.ShiroUtils;
import tv.huan.media.domain.*;
import tv.huan.media.service.IAuthorizerService;
import tv.huan.media.service.IClassificationService;
import tv.huan.media.service.IItemAlbumService;
import tv.huan.media.service.IItemService;
import tv.huan.system.domain.SysUser;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目专辑关系Controller
 * 
 * @author admin
 * @date 2019-10-12
 */
@Controller
@RequestMapping("/media/itemAlbum")
public class ItemAlbumController extends BaseController
{
    private String prefix = "media/itemAlbum";

    @Autowired
    private IItemAlbumService itemAlbumService;

    @Autowired
    private IClassificationService classificationService;

    @Autowired
    private IAuthorizerService authorizerService;

    @Autowired
    private IItemService itemService;

    @RequiresPermissions("media:itemAlbum:view")
    @GetMapping()
    public String itemAlbum(ModelMap mmap)
    {
        mmap.put("classificationList", classificationService.selectClassificationListAll());
        mmap.put("classification", new Classification());
        mmap.put("authorizerList", authorizerService.selectAuthorizerListAll());
        mmap.put("authorizer", new Authorizer());
        mmap.put("itemList", itemService.selectItemList(new Item()));
        mmap.put("item", new Item());
        return prefix + "/itemAlbum";
    }

    /**
     * 查询项目专辑关系列表
     */
    @RequiresPermissions("media:itemAlbum:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ItemAlbum itemAlbum)
    {
        startPage();
        List<ItemAlbum> list = itemAlbumService.selectItemAlbumList(itemAlbum);
        return getDataTable(list);
    }

    /**
     * 导出项目专辑关系列表
     */
    @RequiresPermissions("media:itemAlbum:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ItemAlbum itemAlbum)
    {
        List<ItemAlbum> list = itemAlbumService.selectItemAlbumList(itemAlbum);
        ExcelUtil<ItemAlbum> util = new ExcelUtil<ItemAlbum>(ItemAlbum.class);
        return util.exportExcel(list, "itemAlbum");
    }

    /**
     * 新增项目专辑关系
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 出库
     */
    @RequiresPermissions("media:itemAlbum:outStock")
    @PostMapping("/outStock")
    @ResponseBody
    public AjaxResult outStock(String ids, Integer state)
    {
        List<ItemAlbum> itemAlbumList = itemAlbumService.selectItemAlbumListByIds(ids);
        for (ItemAlbum itemAlbum : itemAlbumList){
            if (1 != itemAlbum.getState()){
                return AjaxResult.error("无法批量出库");
            }
        }
        //当前用户
        SysUser user = ShiroUtils.getSysUser();
        try {
            itemAlbumService.updateItemAlbum(itemAlbumList,user,state,null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("出库成功");
    }

    /**
     * 上线下线等操作
     */
    @RequiresPermissions("media:itemAlbum:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(String ids, Integer state, String operateReason)
    {
        List<ItemAlbum> itemAlbumList = itemAlbumService.selectItemAlbumListByIds(ids);
        for (ItemAlbum itemAlbum : itemAlbumList){
            //批量可上线，校验当前状态为待出库（0）
            if (state == 1){
                if (itemAlbum.getState() != 0){
                    return AjaxResult.error("无法批量可上线");
                }
            }
            //批量不可上线，校验当前状态为待出库（0）
            if (state == -1){
                if (itemAlbum.getState() != 0){
                    return AjaxResult.error("无法批量不可上线");
                }
            }
            //重新可上线，校验当前状态为不可上线（-1） 或者  已下线（4）
            if (state == 11){
                if (itemAlbum.getState() != (-1) && itemAlbum.getState() != 4){
                    return AjaxResult.error("无法重新可上线");
                }
            }
            //批量上线，校验当前状态为已出库（2）或已下线（4）
            if (state == 3){
                if (itemAlbum.getState() != 2 && itemAlbum.getState() != 4 ){
                    return AjaxResult.error("无法批量上线");
                }
            }
            //批量下线，校验当前状态必须为上线（3）
            if (state == 4 && itemAlbum.getState() != 3){
                return AjaxResult.error("无法批量下线");
            }
        }
        //当前用户
        SysUser user = ShiroUtils.getSysUser();
        try {
            itemAlbumService.updateItemAlbum(itemAlbumList,user,state,operateReason);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 操作原因（上线片名，下线理由，不可上线理由）
     */
    @GetMapping("/operateReason/{ids}/{state}")
    public String operateReason(@PathVariable("ids") String ids, @PathVariable("state") Integer state, ModelMap mmap)
    {
        mmap.put("ids", ids);
        mmap.put("state", state);
        return prefix + "/operateReason";
    }

    /**
     * 删除项目专辑关系
     */
    @RequiresPermissions("media:itemAlbum:remove")
    @Log(title = "项目专辑关系", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(itemAlbumService.deleteItemAlbumByIds(ids));
    }


    @RequiresPermissions("media:itemAlbum:settlement")
    @GetMapping("/settlement")
    public String settlement(ModelMap mmap)
    {
        mmap.put("authorizerList", authorizerService.selectAuthorizerListAll());
        mmap.put("authorizer", new Authorizer());
        mmap.put("itemList", itemService.selectItemList(new Item()));
        mmap.put("item", new Item());
        return "media/settlement/settlement";
    }

    /**
     * 结算列表
     */
    @RequiresPermissions("media:itemAlbum:getSettlementList")
    @PostMapping("/getSettlementList")
    @ResponseBody
    public TableDataInfo getSettlementList(Settlement settlement)
    {
        startPage();
        List<Settlement> list;
        if (settlement.getItemId() == null || settlement.getParams().get("month").equals("")){
            list = new ArrayList<>();
        }else {
            list = itemAlbumService.getSettlementList(settlement);
        }
        return getDataTable(list);
    }

    /**
     * 导出结算信息
     */
    @RequiresPermissions("media:itemAlbum:exportSettlement")
    @PostMapping("/exportSettlement")
    @ResponseBody
    public AjaxResult exportSettlement(Settlement settlement)
    {
        List<Settlement> list;
        if (settlement.getItemId() == null || settlement.getParams().get("month").equals("")){
            list = new ArrayList<>();
        }else {
            list = itemAlbumService.getSettlementList(settlement);
        }
        ExcelUtil<Settlement> util = new ExcelUtil<>(Settlement.class);
        return util.exportExcel(list, "settlement");
    }
}
